package qin.view;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import qin.BookInformationSystem;
import qin.entity.Book;
import qin.entity.R;
import qin.service.BookService;
import qin.service.Impl.BookServiceImpl;

/**
 * 图书信息页，展示所有图书信息并提供相关的功能
 * @author 小行星
 *
 */
public class BookInformationView {
	private BookInformationSystem bookInformationSystem;
	private JFrame frame;
	private JTable table;
	private JButton del;
	private JButton edit;
	private JButton search;
	private JButton add;
	private JButton reset;
	private JScrollPane scrollPane;
	private DefaultTableModel model;
	private String[] colName = {"书号", "书名","价格","作者","出版社"};
	private boolean isAdmin = false;
	private BookService bookService;
	private JPanel panel;

	public BookInformationView(BookInformationSystem bookInformationSystem) {
		this.bookInformationSystem = bookInformationSystem;
		this.bookService = BookServiceImpl.build(); 
		initialize();
		// 修改信息
		flushMode();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setTitle("欢迎您，游客");
		frame.setBounds(100, 100, 772, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		model = new DefaultTableModel();
		table = new JTable(model);
		table.setEnabled(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		scrollPane = new JScrollPane(table);
		scrollPane.setBounds(0, 67, 756, 394);
		frame.getContentPane().add(scrollPane);
		
		search = new JButton("搜索");
		search.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int num = JOptionPane.showConfirmDialog(null, "选择【是】搜索书号、选择【否】搜索作者、选择【取消】搜索出版社", "搜索", 
					    JOptionPane.YES_NO_CANCEL_OPTION);
				if(num == 0) {
					String number = (String)JOptionPane.showInputDialog(null, "请输入要搜索的书号", "搜索书号", 2, null, null, null);
					if(number == null) return;
					
					R<Book> r = bookService.getBookByNumber(number);
					if(r.isStatus()) {
						updateModel(Arrays.asList(r.getData()));
					}else {
						JOptionPane.showMessageDialog(null, "查询失败！"+r.getMsg(), "搜索书号", 2);
					}
				}else if(num == 1) {
					String author = (String)JOptionPane.showInputDialog(null, "请输入要搜索的作者", "搜索作者", 2, null, null, null);
					if(author == null) return;
					R<List<Book>> r = bookService.getBooksByAuthor(author);
					updateModel(r.getData());
				}else if(num == 2) {
					String press = (String)JOptionPane.showInputDialog(null, "请输入要搜索的出版社", "搜索出版社", 2, null, null, null);
					if(press == null) return;
					R<List<Book>> r = bookService.getBooksByPress(press);
					updateModel(r.getData());
				}
			}
		});
		search.setBounds(22, 5, 83, 23);
		
		edit = new JButton("编辑");
		edit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(!isAdmin) {
					JOptionPane.showMessageDialog(null, "游客无法修改图书！", "修改图书", 2);
					return;
				}
				// 输入要修改的号
				String number = (String)JOptionPane.showInputDialog(null, "请输入要修改的书号", "修改图书", 2, null, null, null);
				if(number == null) return;
				
				R<Book> r = bookService.getBookByNumber(number);
				if(r.isStatus()) {
					BookInputView inputView = new BookInputView(bookInformationSystem);
					inputView.showEdit(r.getData());
				}else {
					JOptionPane.showMessageDialog(null, "查询失败！"+r.getMsg(), "修改图书", 2);
				}
			}
		});
		edit.setBounds(143, 5, 83, 23);
		
		del = new JButton("删除");
		del.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(!isAdmin) {
					JOptionPane.showMessageDialog(null, "游客无法删除图书！", "修改图书", 2);
					return;
				}
				// 输入要删除的号
				String number = (String)JOptionPane.showInputDialog(null, "请输入要删除的书号", "删除图书", 2, null, null, null);
				if(number == null) return;
				R<?> r = bookService.deleteBook(number);
				if(r.isStatus()) {
					JOptionPane.showMessageDialog(null, "删除成功！", "删除图书", 2);
					flushMode();
				}else {
					JOptionPane.showMessageDialog(null, "删除失败！"+r.getMsg(), "删除图书", 2);
				}
			}
		});
		del.setBounds(266, 5, 83, 23);
		
		add = new JButton("添加");
		add.setBounds(377, 5, 83, 23);
		add.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(!isAdmin) {
					JOptionPane.showMessageDialog(null, "游客无法添加图书！", "添加图书", 2);
					return;
				}
				BookInputView inputView = new BookInputView(bookInformationSystem);
				inputView.showAdd();
			}
		});
		
		
		panel = new JPanel();
		panel.setBounds(0, 10, 756, 47);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		panel.add(search);
		
		reset = new JButton("刷新");
		reset.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				flushMode();
			}
		});
		reset.setBounds(673, 0, 83, 47);
		panel.add(reset);
	}
	
	public void switchVisable() {
		frame.setVisible(!frame.isVisible());
	}
	
	public void showAdminBookInformationView() {
		isAdmin = true;
		frame.setTitle("欢迎您，管理员");
		panel.add(edit);
		panel.add(del);
		panel.add(add);
	}
	
	private void updateModel(List<Book> data) {
		Object[][] obj = new Object[data.size()][5];
		for(int i = 0; i < data.size(); i++) {
			obj[i][0] = data.get(i).getNumber();
			obj[i][1] = data.get(i).getName();
			obj[i][2] = data.get(i).getPrice();
			obj[i][3] = data.get(i).getAuthor();
			obj[i][4] = data.get(i).getPress();
		}
		
		model.setDataVector(obj, colName);
	}
	
	public void flushMode() {
		updateModel(bookService.getBooks().getData());
	}
}
